# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# Contributor: wirx6 <wirx654@gmail.com>

_realname=Vulkan-ValidationLayers
pkgbase=mingw-w64-vulkan-validation-layers
pkgname=("${MINGW_PACKAGE_PREFIX}-vulkan-validation-layers")
pkgver=1.3.261
pkgrel=2
pkgdesc='Vulkan Validation Layers (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.khronos.org/vulkan/"
groups=("${MINGW_PACKAGE_PREFIX}-vulkan-devel")
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
         "${MINGW_PACKAGE_PREFIX}-spirv-tools")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-glslang"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "${MINGW_PACKAGE_PREFIX}-spirv-headers"
             "${MINGW_PACKAGE_PREFIX}-robin-hood-hashing")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/KhronosGroup/${_realname}/archive/v${pkgver}.tar.gz"
        "001-revert-Add-utility-libraries-repository-dependence.patch::https://github.com/KhronosGroup/Vulkan-ValidationLayers/commit/e6bdb8d7.patch")
sha256sums=('ab769d9d7550e1636c9309387a7e53be5ba89f0b19f810bb40caa1b6eaefe8ee'
            'b9d15c4ad8c0e4eb7b5dc21de3ee8442bf142578bd5daf0ea3b8ff413b59ec12')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    001-revert-Add-utility-libraries-repository-dependence.patch
}

build() {
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ "${MSYSTEM}" == "MINGW32" ]]; then
    # https://www.mail-archive.com/devel@lists.fedoraproject.org/msg167502.html
    extra_config+=("-DCMAKE_CXX_FLAGS_RELEASE=-O1 -DNDEBUG")
  fi

  CXXFLAGS+=" -Wno-unused-function" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    "${extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DGLSLANG_INSTALL_DIR=${MINGW_PREFIX} \
    -DSPIRV_HEADERS_INSTALL_DIR=${MINGW_PREFIX} \
    -DBUILD_LAYER_SUPPORT_FILES=ON \
    -DBUILD_TESTS=OFF \
    -DBUILD_WERROR=OFF \
    -DUSE_ROBIN_HOOD_HASHING=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd ${srcdir}/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -dm755 ${pkgdir}${MINGW_PREFIX}/share/doc/vulkan-validation-layers/
  cp -r ${srcdir}/${_realname}-${pkgver}/docs/* ${pkgdir}${MINGW_PREFIX}/share/doc/vulkan-validation-layers/
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE.TXT ${pkgdir}${MINGW_PREFIX}/share/licenses/vulkan-validation-layers/LICENSE
}
